
document.addEventListener('DOMContentLoaded', function () {
    // 1. 自我描述编辑功能
    const editDescBtn = document.getElementById('editDescBtn');
    const saveDescBtn = document.getElementById('saveDescBtn');
    const cancelDescBtn = document.getElementById('cancelDescBtn');
    const descEditGroup = document.getElementById('descEditGroup');
    const descInput = document.getElementById('descInput');
    const userDescText = document.getElementById('userDescText');

    editDescBtn.addEventListener('click', function () {
        descInput.value = userDescText.textContent;
        userDescText.parentElement.style.display = 'none';
        descEditGroup.style.display = 'block';
    });

    saveDescBtn.addEventListener('click', function () {
        userDescText.parentElement.style.display = 'flex';
        descEditGroup.style.display = 'none';
        edit_user_description(descInput.value);
    });

    cancelDescBtn.addEventListener('click', function () {
        userDescText.parentElement.style.display = 'flex';
        descEditGroup.style.display = 'none';
    });

    // 2. 交易地址编辑功能
    const editAddressBtn = document.getElementById('editAddressBtn');
    const saveAddressBtn = document.getElementById('saveAddressBtn');
    const cancelAddressBtn = document.getElementById('cancelAddressBtn');
    const addressEditGroup = document.getElementById('addressEditGroup');
    const addressInput = document.getElementById('addressInput');
    const addressText = document.getElementById('addressText');

    editAddressBtn.addEventListener('click', function () {
        addressInput.value = addressText.textContent;
        addressText.parentElement.style.display = 'none';
        addressEditGroup.style.display = 'block';
    });

    saveAddressBtn.addEventListener('click', function () {
        addressText.parentElement.style.display = 'flex';
        addressEditGroup.style.display = 'none';
        edit_user_address(addressInput.value.trim());
    });

    cancelAddressBtn.addEventListener('click', function () {
        addressText.parentElement.style.display = 'flex';
        addressEditGroup.style.display = 'none';
    });

    // 3. 头像上传功能
    const avatarUpload = document.getElementById('avatarUpload');
    const changeAvatarBtn = document.getElementById('changeAvatarBtn');
    const avatarPreview = document.querySelector('.profile-avatar');

    changeAvatarBtn.addEventListener('click', function () {
        avatarUpload.click();
    });

    avatarUpload.addEventListener('change', function (e) {
        const file = e.target.files[0];
        if (file && file.type.match('image.*')) {
            upload_avatar(file);
        }
    });

    // 4. 发布商品按钮
    document.getElementById('publishBtn').addEventListener('click', function () {
        // window.location.href = "/publish";
        window.location.href = "/publish.html";
    });

    load_user_info();
});

async function load_user_info() {
    const response = await fetch('/api/user/profile',
        {
            headers: {
                'Content-Type': 'application/json'
            }
        }
    );
    const data = await response.json();
    // 头像
    document.getElementById('avatar').src = data.avatar;
    // 昵称
    document.getElementById('nickname').textContent = data.nickname;
    // 邮箱
    document.getElementById('userEmail').textContent = data.email;
    // 自我描述
    document.getElementById('userDescText').textContent = data.description;
    // 交易地址
    document.getElementById('addressText').textContent = data.address;
}

async function upload_avatar(file) {
    const formData = new FormData();
    formData.append('image', file);
    const response = await fetch('/api/user/avatar', {
        method: 'POST',
        body: formData
    });
    const data = await response.json();
    if (data.status == 'success') {
        const avatarPreview = document.querySelector('.profile-avatar');
        avatarPreview.src = data.avatar;
    } else {
        alert(data.message);
    }
}

async function edit_user_address(address) {
    const response = await fetch('/api/user/address', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
        },
        body: JSON.stringify({ address })
    });
    const data = await response.json();
    if (data.status == 'success') {
        document.getElementById('addressText').textContent = address;
    } else {
        alert(data.message);
    }
}

async function edit_user_description(description) {
    const response = await fetch('/api/user/description', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
        },
        body: JSON.stringify({ description })
    });
    const data = await response.json();
    if (data.status == 'success') {
        document.getElementById('userDescText').textContent = description;
    } else {
        alert(data.message);
    }
}

